package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.gypsumCompete;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dto.CustomerBidDto;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.CustomerBidExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.ProductWeightMoneyNumExportExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.GetAllCustomerBidVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.GetAllGypsumCompeteVipCustomerVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.GetAllGypsumCompetesVo;
import com.xiaoxu.intranetweb.util.SzResult;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;

@Component
public class GypsumCompeteBu {
    @Autowired
    private ITGypsumCompeteService gypsumCompeteService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private ITGypsumCompeteTenderService gypsumCompeteTenderService;
    @Autowired
    private ITGypsumCompeteVipService  gypsumCompeteVipService;
    @Autowired
    private ICCompanysService  cCompanysService;
    @Autowired
    private ICProductsInfoService icProductsInfoService;
    @Autowired
    private GypsumCompeteUtil gypsumCompeteUtil;
    @Autowired
    private ITSpecialPriceTypeService specialPriceTypeService;
    @Autowired
    private ITSpecialPriceService tSpecialPriceService;
    @Autowired
    private SysLogBu sysLogBu;

    public SzResult getAllGypsumCompetes(Integer page1, Integer pageSize, String title, String startTime, String endTime, Integer bidType, String status, String plantId) {
        Page<TGypsumCompete> page = new Page<>(page1, pageSize);
        QueryWrapper<TGypsumCompete> queryWrapper = new QueryWrapper<>();
        if (title != null) {
            queryWrapper.like(!title.equals(""), "TITLE", title);
        }
                if (startTime != null & endTime != null) {
            queryWrapper.ge(!startTime.equals(""), "BEGIN_T_DATE", startTime);
        }
        if (startTime != null & endTime != null) {
            queryWrapper.le(!endTime.equals(""), "BEGIN_DATE", endTime);
        }
        queryWrapper.eq(bidType != null, "BID_TYPE", bidType);
        queryWrapper.eq(status != null, "STATUS", status);
        queryWrapper.eq("DEL_FLAG", 0);
        queryWrapper.eq("ARCHIVED", 0);
        queryWrapper.eq("DATA_FROM",plantId);
        queryWrapper.orderByDesc("BEGIN_DATE");
        gypsumCompeteService.page(page, queryWrapper);
        Long total = page.getTotal();
        List<TGypsumCompete> list = page.getRecords();
        List<GetAllGypsumCompetesVo> vos = new ArrayList<>();
        for (TGypsumCompete gypsumCompete : list
        ) {
            GetAllGypsumCompetesVo vo = new GetAllGypsumCompetesVo();
            vo.setId(gypsumCompete.getId());
            vo.setTitle(gypsumCompete.getTitle());
            vo.setBidType(gypsumCompete.getBidType());
            vo.setStatus(gypsumCompete.getStatus());
            vo.setArchived(gypsumCompete.getArchived());
            vo.setContent(gypsumCompete.getContent());
            vo.setApproverId(gypsumCompete.getApproverId());
            vo.setBeginDate(gypsumCompete.getBeginDate());
            vo.setBeginTDate(gypsumCompete.getBeginTDate());
            vo.setBidMonth(gypsumCompete.getBidMonth());
            vo.setDataFrom(gypsumCompete.getDataFrom());
            vo.setDelFlag(gypsumCompete.getDelFlag());
            vo.setMaxDays(gypsumCompete.getMaxDays());
            vo.setMinBid(gypsumCompete.getMinBid());
            vo.setApproveTime(gypsumCompete.getApproveTime());
            vo.setPdtId(gypsumCompete.getPdtId());
            vo.setIfConfirm(gypsumCompete.getIfConfirm());
            if (gypsumCompete.getPdtId() !=null){
                if (!gypsumCompete.getPdtId().equals("999")) {
                    vo.setPdtName(icProductsInfoService.getById(gypsumCompete.getPdtId()).getPdtinfPdt());
                }else {
                    vo.setPdtName("全部");
                }
            }
            if (gypsumCompete.getApproverId() != null) {
                vo.setApprovered(1);
                vo.setApproverName(sysUserService.getById(gypsumCompete.getApproverId()).getTrueName());
            }
            vo.setBidDays(gypsumCompete.getBidDays());
            vo.setPriceShow(gypsumCompete.getPriceShow());
            vo.setCreateBy(gypsumCompete.getCreateBy());
            vo.setCreateDate(gypsumCompete.getCreateDate());
            if (gypsumCompete.getCreateBy() != null) {
                vo.setCreateName(sysUserService.getById(gypsumCompete.getCreateBy()).getTrueName());
            }
            vos.add(vo);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("list", vos);
        return SzResult.success(map);
    }

    //   新增竞标
    public SzResult addGypsumCompete(
            String title,
            String month,
            String beginDate,
            String beginTDate,
            Integer bidDays,
            Double minBid,
            Integer maxDays,
//            Integer bidType,
            Integer priceShow,
            String pdtId,
            String token,
            String plantId) throws Exception {
        SysUser sysUser = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        TGypsumCompete gypsumCompete = new TGypsumCompete();
        gypsumCompete.setTitle(title);
        gypsumCompete.setBidMonth(month);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 根据实际格式调整
        gypsumCompete.setBeginDate(LocalDateTime.parse(beginDate.replace("T", " "),formatter));
        gypsumCompete.setBeginTDate(LocalDateTime.parse(beginTDate.replace("T", " "),formatter));
        gypsumCompete.setBidDays(bidDays);
        gypsumCompete.setMinBid(minBid);
        gypsumCompete.setMaxDays(maxDays);
//        gypsumCompete.setBidType(bidType);
        gypsumCompete.setPriceShow(priceShow);
        gypsumCompete.setCreateDate(LocalDateTime.now());
        gypsumCompete.setCreateBy(sysUser.getId());
//        根据品种去组织t_gypsum_compete_detail表的数据
        gypsumCompete.setPdtId(pdtId);
        gypsumCompete.setDataFrom(plantId);
        gypsumCompeteService.save(gypsumCompete);
        return SzResult.Success();
    }

    //   审批，就是将status变成10
    public SzResult approveGypsumCompete(
            String id,
            Integer status,
            String token,
            String plantId) throws Exception {
        QueryWrapper<TGypsumCompete> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("DATA_FROM", plantId);
        TGypsumCompete gypsumCompete1 = gypsumCompeteService.getOne(queryWrapper);
        if (gypsumCompete1.getApproverId() !=null){
            return SzResult.Error("已经审批过了");
        }
        SysUser sysUser = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        TGypsumCompete gypsumCompete = gypsumCompeteService.getById(id);
        gypsumCompete.setStatus(status);
        gypsumCompete.setApproverId(sysUser.getId());
        gypsumCompete.setApproveTime(LocalDateTime.now());
        gypsumCompeteService.updateById(gypsumCompete);
        return SzResult.Success();
    }
//    撤销标，在投标开始时间之前，等于删掉数据
    public SzResult revokeGypsumCompete(
            String id,
            String token,
            String plantId) throws Exception {
        QueryWrapper<TGypsumCompete> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("DATA_FROM", plantId);
        TGypsumCompete gypsumCompete1 = gypsumCompeteService.getOne(queryWrapper);
        if (gypsumCompete1.getBeginTDate().isAfter(LocalDateTime.now())) {
            return SzResult.Error("已经过了截止日期，无法撤销");
        }
        SysUser sysUser = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        TGypsumCompete gypsumCompete = gypsumCompeteService.getById(id);
        gypsumCompete.setDelFlag(1);
        gypsumCompete.setApproverId(sysUser.getId());
        gypsumCompete.setApproveTime(LocalDateTime.now());
        gypsumCompeteService.updateById(gypsumCompete);
        return SzResult.Success();
    }

    //    修改招标信息，在未到开始投标之前
    public SzResult updateGypsumCompete(
            String id,
            String title,
            String beginDate,
            String beginTDate,
            Integer bidDays,
            String pdtId,
            Double minBid,
            Integer maxDays,
            Integer bidType,
            Integer priceShow,
            String token,
            String plantId) throws Exception {
//        先查一下这个标，如果已经过了开标时间就不给改了
        TGypsumCompete gypsumCompete1 = gypsumCompeteService.getById(id);
        if (gypsumCompete1.getBeginTDate().isBefore(LocalDateTime.now())) {
            SzResult.Error("已经过了截止日期，无法修改");
        }
        SysUser sysUser = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        TGypsumCompete gypsumCompete = gypsumCompeteService.getById(id);
        gypsumCompete.setTitle(title);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); // 根据实际格式调整
        gypsumCompete.setBeginDate(LocalDateTime.parse(beginDate.replace("T", " "),formatter));
        gypsumCompete.setBeginTDate(LocalDateTime.parse(beginTDate.replace("T", " "),formatter));
        gypsumCompete.setBidDays(bidDays);
        gypsumCompete.setPdtId(pdtId);
        gypsumCompete.setMinBid(minBid);
        gypsumCompete.setMaxDays(maxDays);
        gypsumCompete.setBidType(bidType);
        gypsumCompete.setPriceShow(priceShow);
        gypsumCompete.setDataFrom(plantId);
        gypsumCompeteService.updateById(gypsumCompete);
        return SzResult.Success();
    }
//    归档
    public SzResult archiveGypsumCompete(
            String id,
            Integer archived) {
        TGypsumCompete gypsumCompete = gypsumCompeteService.getById(id);
        gypsumCompete.setArchived(archived);
        gypsumCompeteService.updateById(gypsumCompete);
        return SzResult.Success();
    }

    //    客户投标
    public SzResult customerBid(
            List<CustomerBidDto> customerBidDtos,String token,String plantId) throws Exception {
        for (CustomerBidDto customerBidDto : customerBidDtos
             ) {
            List<CCompanys> companys = userInfoDao.getUserCompany(StpUtil.getLoginIdByToken(token).toString());
            TGypsumCompeteTender tender = new TGypsumCompeteTender();
            tender.setCpyId(companys.get(0).getId());
            tender.setPrice(customerBidDto.getPrice());
            tender.setDay(customerBidDto.getDay());
            tender.setPdtId(customerBidDto.getPdtId());
            tender.setCompeteId(customerBidDto.getCompeteId());
            tender.setCreateTime(LocalDateTime.now());
            tender.setDataFrom(plantId);
            gypsumCompeteTenderService.save(tender);
        }
        return SzResult.Success();
    }

//修改确认信息
    public SzResult updateConfirmCustomerBid(
            String id,
            Integer winBid,
            BigDecimal winBidPrice,
//            Integer winBidDay,
            String token,
            String plantId) throws Exception {
        SysUser sysUser = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        TGypsumCompeteTender gypsumCompeteTender = gypsumCompeteTenderService.getById(id);
        gypsumCompeteTender.setWinBid(winBid);
        gypsumCompeteTender.setWinBidPrice(winBidPrice);
//        gypsumCompeteTender.setWinBidDay(winBidDay);
        gypsumCompeteTender.setCompeteAuditor(sysUser.getId());
        gypsumCompeteTender.setCompeteAuditTime(LocalDateTime.now());
        gypsumCompeteTenderService.updateById(gypsumCompeteTender);
        return SzResult.Success();
    }

//    返回所有投标数据
    public SzResult getAllCustomerBid(
            Integer page,
            Integer pageSize,
            String pdtId,
            String coId,
            String competeId,
            Integer day,
            String plantId) throws Exception {
        Page<TGypsumCompeteTender> page1 = new Page<>(page, pageSize);
        QueryWrapper<TGypsumCompeteTender> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("DATA_FROM", plantId);
        if (pdtId != null) {
            queryWrapper.eq(!pdtId.equals(""), "PDT_ID", pdtId);
        }
        if (coId != null) {
            queryWrapper.eq(!coId.equals(""), "CO_ID", coId);
        }
        if (competeId != null) {
            queryWrapper.eq("COMPETE_ID", competeId);
        }
//        if (day != null) {
//            queryWrapper.eq("DAY", day);
//        }
        queryWrapper.orderByDesc("PRICE");
        // 添加多列排序逻辑
        queryWrapper.orderByDesc("PRICE")
                .orderByDesc("DAY") // 价格相同的情况下按天数降序
                .orderByAsc("CREATE_TIME"); // 价格和天数都相同的情况下按创建时间升序
        IPage<TGypsumCompeteTender> list = gypsumCompeteTenderService.page(page1, queryWrapper);
        List<TGypsumCompeteTender> gypsumCompeteTenders = list.getRecords();
        List<GetAllCustomerBidVo> customerBidVos = new ArrayList<>();
        for (TGypsumCompeteTender gypsumCompeteTender:gypsumCompeteTenders){
            GetAllCustomerBidVo vo = new GetAllCustomerBidVo();
            vo.setId(gypsumCompeteTender.getId());
            vo.setCpyId(gypsumCompeteTender.getCpyId());
            CCompanys cCompanys = cCompanysService.getById(gypsumCompeteTender.getCpyId());
            vo.setCpyName(cCompanys.getCpyCo()+"."+cCompanys.getCpyChico());
            vo.setPdtId(gypsumCompeteTender.getPdtId());
            if (gypsumCompeteTender.getPdtId() !=null){
                if (!gypsumCompeteTender.getPdtId().equals("999")) {
                    vo.setPdtName(icProductsInfoService.getById(gypsumCompeteTender.getPdtId()).getPdtinfPdt());
                }else {
                    vo.setPdtName("全部");
                }
            }
            vo.setCompeteId(gypsumCompeteTender.getCompeteId());
            vo.setDay(gypsumCompeteTender.getDay());
            vo.setPrice(gypsumCompeteTender.getPrice());
            vo.setCompeteAuditor(gypsumCompeteTender.getCompeteAuditor());
            vo.setCompeteAuditorName(gypsumCompeteTender.getCompeteAuditorName());
            vo.setCompeteAuditTime(gypsumCompeteTender.getCompeteAuditTime());
            vo.setCreateTime(gypsumCompeteTender.getCreateTime());
            vo.setDataFrom(gypsumCompeteTender.getDataFrom());
            QueryWrapper<TGypsumCompete> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("ID", competeId);
            queryWrapper1.eq("DATA_FROM", plantId);
            TGypsumCompete gypsumCompete1 = gypsumCompeteService.getOne(queryWrapper1);
            if (gypsumCompete1.getIfConfirm() ==1){
                vo.setWinBid(gypsumCompeteTender.getWinBid());
                vo.setWinBidPrice(gypsumCompeteTender.getWinBidPrice());
                vo.setWinBidDay(gypsumCompeteTender.getDay());
            }

            customerBidVos.add(vo);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("totle",customerBidVos.size());
        map.put("list", customerBidVos);
      return   SzResult.success(map);
    }


//    查询所有石膏竞价vip客户的数据
    public SzResult getAllGypsumCompeteVipCustomer(Integer page,Integer pageSize,String pdtId,String coId,Integer month,String plantId)  {
        Page<TGypsumCompeteVip> page1 = new Page<>(page, pageSize);
        QueryWrapper<TGypsumCompeteVip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("DATA_FROM", plantId);
        if (pdtId != null) {
            queryWrapper.eq(!pdtId.equals(""), "PDT_ID", pdtId);
        }
        if (coId != null) {
            queryWrapper.eq(!coId.equals(""), "CO_ID", coId);
        }
        if (month != null) {
            queryWrapper.eq( "MONTHS", month);
        }
        queryWrapper.orderByDesc("CREATE_TIME");
        IPage<TGypsumCompeteVip> list = gypsumCompeteVipService.page(page1, queryWrapper);
        List<TGypsumCompeteVip> gypsumCompeteVips = list.getRecords();
        List<GetAllGypsumCompeteVipCustomerVo> gypsumCompeteVips1 = new ArrayList<>();
        for ( TGypsumCompeteVip gypsumCompeteVip : gypsumCompeteVips
        ) {
            GetAllGypsumCompeteVipCustomerVo vo = new GetAllGypsumCompeteVipCustomerVo();
            vo.setId(gypsumCompeteVip.getId());
            vo.setCoId(gypsumCompeteVip.getCoId());
            if (gypsumCompeteVip.getCoId() !=null){
                CCompanys cCompanys = cCompanysService.getById(gypsumCompeteVip.getCoId());
                vo.setCoName(cCompanys.getCpyCo()+"."+cCompanys.getCpyChico());
            }
            vo.setPdtId(gypsumCompeteVip.getPdtId());
            if ( gypsumCompeteVip.getPdtId() !=null){
                if (!gypsumCompeteVip.getPdtId().equals("999")) {
                    CProductsInfo productsInfo = icProductsInfoService.getById(gypsumCompeteVip.getPdtId());
                    vo.setPdtName(productsInfo.getPdtinfPdt());
                }else {
                    vo.setPdtName("全部");
                }
            }
            vo.setPrice(gypsumCompeteVip.getPrice());
            vo.setDays(gypsumCompeteVip.getDays());
            vo.setMonths(gypsumCompeteVip.getMonths());
            vo.setCreateTime(gypsumCompeteVip.getCreateTime());
            vo.setCreaterId( gypsumCompeteVip.getCreaterId());
            vo.setCreaterName(sysUserService.getById(gypsumCompeteVip.getCreaterId()).getTrueName());
            vo.setIfExecute(gypsumCompeteVip.getIfExecute());
            vo.setDataFrom(gypsumCompeteVip.getDataFrom());
            gypsumCompeteVips1.add(vo);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", list.getTotal());
        map.put("list", gypsumCompeteVips1);
        return SzResult.success(map);
    }


//    exportCustomerBidExcel投标数据的excel
    @Async
    public Future<List<CustomerBidExcelEn>> exportCustomerBidExcel(
            String competeId) {
        QueryWrapper<TGypsumCompeteTender> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("COMPETE_ID", competeId);
//        if (day != null) {
//            queryWrapper.eq("DAY", day);
//        }
        queryWrapper.orderByDesc("PRICE");
        // 添加多列排序逻辑
        queryWrapper.orderByDesc("PRICE")
                .orderByDesc("DAY") // 价格相同的情况下按天数降序
                .orderByAsc("CREATE_TIME"); // 价格和天数都相同的情况下按创建时间升序
        List<TGypsumCompeteTender> list = gypsumCompeteTenderService.list(queryWrapper);
        List<CustomerBidExcelEn> customerBidExcelEns = new ArrayList<>();
        for (TGypsumCompeteTender gypsumCompeteTender : list
        ) {
            CCompanys cCompanys = cCompanysService.getById(gypsumCompeteTender.getCpyId());
            String winBidStatus;
            if (gypsumCompeteTender.getWinBid() == null) {
                winBidStatus = "未审";
            } else if (gypsumCompeteTender.getWinBid() == 1) {
                winBidStatus = "中标";
            } else if (gypsumCompeteTender.getWinBid() == 2) {
                winBidStatus = "未中";
            } else {
                winBidStatus = "未知状态"; // 处理非预期的数字情况
            }
            CustomerBidExcelEn vo = CustomerBidExcelEn.builder()
                    // 对于字符串类型的字段，使用 Objects.toString() 防止空指针，并提供一个默认空字符串""
                    .cpyName(Objects.toString(cCompanys.getCpyCo(), "") + "." + Objects.toString(cCompanys.getCpyChico(), ""))
                    .price(gypsumCompeteTender.getPrice() != null ? gypsumCompeteTender.getPrice() : BigDecimal.valueOf(0))
                    .days(gypsumCompeteTender.getDay() != null ? gypsumCompeteTender.getDay() : 0)
                    .winPrice(gypsumCompeteTender.getWinBidPrice() != null ? gypsumCompeteTender.getWinBidPrice() : BigDecimal.valueOf(0))
                    .winDay(gypsumCompeteTender.getWinBidDay() != null ? gypsumCompeteTender.getWinBidDay() : 0)
                    // 对于日期时间，同样做非空检查，这里假设使用当前时间作为默认值
                    .createrTime(gypsumCompeteTender.getCreateTime() != null ? gypsumCompeteTender.getCreateTime() : LocalDateTime.now())
                    // 对于winBidStatus也采用同样的处理方式
                    .winBid(Objects.toString(winBidStatus, ""))
                    .build();
            customerBidExcelEns.add(vo);
        }
        return CompletableFuture.completedFuture(customerBidExcelEns);
    }

    //    新增vip客户
    public SzResult addGypsumCompeteVipCustomer(
            String coId,
            String pdtId,
            BigDecimal price,
            Integer days,
            String months,
            String token,
            String plantId) throws Exception {
        QueryWrapper<TGypsumCompeteVip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CO_ID", coId);
        queryWrapper.eq("PDT_ID", pdtId);
        queryWrapper.eq("MONTHS", months);
        queryWrapper.eq("DATA_FROM", plantId);
        List<TGypsumCompeteVip> gypsumCompeteVip1 = gypsumCompeteVipService.list(queryWrapper);
        if (gypsumCompeteVip1.size()>0){
            SzResult.Error("该月份，该单位该品种已有数据");
        }
        SysUser sysUser = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        TGypsumCompeteVip gypsumCompeteVip = new TGypsumCompeteVip();
        gypsumCompeteVip.setCoId(coId);
        gypsumCompeteVip.setPdtId(pdtId);
        gypsumCompeteVip.setPrice(price);
        gypsumCompeteVip.setDays(days);
        gypsumCompeteVip.setMonths(months);
        gypsumCompeteVip.setCreateTime(LocalDateTime.now());
        gypsumCompeteVip.setCreaterId(sysUser.getId());
        gypsumCompeteVip.setDataFrom(plantId);
        gypsumCompeteVipService.save(gypsumCompeteVip);
        return SzResult.Success();
    }

    //    手动执行
    @Transactional(rollbackFor = Exception.class)
    public SzResult executeGypsumCompeteVipCustomer(
            String id,
            String token,
            String plantId) throws Exception {
        SysUser sysUser = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        TGypsumCompeteVip gypsumCompeteVip = gypsumCompeteVipService.getById(id);
        if ( gypsumCompeteVip.getIfExecute() ==1){
            return SzResult.Error("已经执行过了");
        }
        gypsumCompeteVip.setIfExecute(1);
        gypsumCompeteVipService.updateById(gypsumCompeteVip);
        for (int i = 0; i < gypsumCompeteVip.getDays(); i++) {
        String day =  gypsumCompeteUtil.randomDays(gypsumCompeteVip.getMonths(),gypsumCompeteVip.getPdtId(),"facc577e10c811ef823f00ff8a757bed",plantId);
        if (day == null) continue;
        TSpecialPrice tSpecialPrice = new TSpecialPrice();
        tSpecialPrice.setPriceTypeId("facc577e10c811ef823f00ff8a757bed");
        TSpecialPriceType tSpecialPriceType = specialPriceTypeService.getById("facc577e10c811ef823f00ff8a757bed");
        tSpecialPrice.setPriceType(tSpecialPriceType.getPriceTypeName());
        tSpecialPrice.setCoId(gypsumCompeteVip.getCoId());
        tSpecialPrice.setPdtId(gypsumCompeteVip.getPdtId());
        tSpecialPrice.setExecuteType(1);
        tSpecialPrice.setPrice(gypsumCompeteVip.getPrice());
        tSpecialPrice.setNum(BigDecimal.valueOf(99));
        tSpecialPrice.setResidueNum(BigDecimal.valueOf(99));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        tSpecialPrice.setStartTime(LocalDateTime.parse(day,formatter));
//                    把day改为23：59：59结尾
        String endTime = day.substring(0,10)+" 23:59:59";
        tSpecialPrice.setEndTime(LocalDateTime.parse(endTime,formatter));
        tSpecialPrice.setCreateTime(LocalDateTime.now());
        tSpecialPrice.setCreaterId(sysUser.getId());
        tSpecialPrice.setConnId(gypsumCompeteVip.getId());
        tSpecialPrice.setDataFrom(plantId);
        tSpecialPriceService.save(tSpecialPrice);
        }
        return SzResult.Success();
    }
    //    删除vip客户
    @Transactional(rollbackFor = Exception.class)
    public SzResult deleteGypsumCompeteVipCustomer(
            String id,
            String token,
            String plantId) throws Exception {
        SysUser sysUser = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
           TGypsumCompeteVip gypsumCompeteVip = gypsumCompeteVipService.getById(id);
           gypsumCompeteVipService.removeById(id);
//        将tSpecialPriceService表的DEL_FLAG字段改成1
        List<TSpecialPrice> tSpecialPrices = tSpecialPriceService.list(new QueryWrapper<TSpecialPrice>().eq("CONN_ID", id));
        for (TSpecialPrice tSpecialPrice : tSpecialPrices) {
            tSpecialPrice.setDelFlag(1);
            tSpecialPriceService.updateById(tSpecialPrice);
        }
        CCompanys cCompanys = cCompanysService.getById(gypsumCompeteVip.getCoId());
        sysLogBu.logRecord("石膏竞价",sysUser.getTrueName(),"删除了石膏竞价特供单位【"+cCompanys.getCpyCo()+"."+cCompanys.getCpyChico()+"】"+gypsumCompeteVip.getMonths()+"月的"+gypsumCompeteVip.getDays()+"天的特供数据",plantId);
        return SzResult.Success();
    }


}
